/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Register.java
 *
 * Created on 08/04/2011, 16:42:44
 */

package GUI;

import Business.FrameHandler;
import Domain.DataManager;
import Domain.UserClient.ButtonsFunctions;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Vector;
import objects.Frm;

/**
 *
 * @author Tal Cohen
 */
public class Register extends javax.swing.JFrame {

    public static void changeErrLable(String string) {
        availLabel.setText(string);
        availLabel.setVisible(true);

    }

    /** Creates new form Register */
    public Register() {
        initComponents();
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);
        availLabel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userLabel = new javax.swing.JLabel();
        pass1Label = new javax.swing.JLabel();
        userField = new javax.swing.JTextField();
        pass2Label = new javax.swing.JLabel();
        registerBut = new javax.swing.JButton();
        CheckAvailBut = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        pass1Field = new javax.swing.JPasswordField();
        pass2Field = new javax.swing.JPasswordField();
        errorLabel = new javax.swing.JLabel();
        backBut = new javax.swing.JButton();
        availLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registration");

        userLabel.setText("Desired Username:");

        pass1Label.setText("Choose a password: ");

        pass2Label.setText("Re-enter password:");

        registerBut.setText("Register");
        registerBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerButActionPerformed(evt);
            }
        });

        CheckAvailBut.setText("Check availability!");
        CheckAvailBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CheckAvailButActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setForeground(new java.awt.Color(153, 0, 255));
        jLabel1.setText("Create an Account ");

        errorLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

        backBut.setText("Back");
        backBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButActionPerformed(evt);
            }
        });

        availLabel.setText("availabilityLabel");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(backBut)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(registerBut))
                            .addComponent(errorLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(availLabel)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pass1Label)
                            .addComponent(userLabel)
                            .addComponent(pass2Label))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(userField, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(pass1Field)
                                .addComponent(pass2Field, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                                .addComponent(CheckAvailBut)))))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(availLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userLabel)
                    .addComponent(userField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CheckAvailBut)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pass1Label)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pass1Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(pass2Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pass2Label))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(registerBut)
                    .addComponent(backBut))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void registerButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerButActionPerformed

        if(pass1Field.getText().equals(pass2Field.getText()))
        {
                    if(pass1Field.getText().isEmpty()){
                        String error = "password can't be empty";
                        FrameHandler.getFrameHandler().showErrorMessage(error);
                        return;
                    }

               ButtonsFunctions.getButtonsFunctions().processRegisterUser(userField.getText(),pass1Field.getText());
                if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                {
                    return;
                }
               ButtonsFunctions.getButtonsFunctions().processConnect(userField.getText(),pass1Field.getText());
                if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                {
                    return;
                }
               ButtonsFunctions.getButtonsFunctions().processAddUser();
                if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                {
                    return;
                    
                }
                ButtonsFunctions.getButtonsFunctions().processGetForums();

                Vector<Frm> forums = DataManager.getDataManager().getVectorForums();

                Vector<String> forumsStringVec = new Vector<String>();
                
                for (int i = 1; i < forums.size() + 1; i++)
                {
                        forumsStringVec.addElement(forums.elementAt(i-1).getForumName());
                }

                ButtonsFunctions.getButtonsFunctions().processGetTypeOfUser();

                int type = DataManager.getDataManager().getTypeOfUser();
                FrameHandler.getFrameHandler().changeToOtherTypeOfFrame(FrameHandler.TOOLBAR);
                FrameHandler.getFrameHandler().changeCurrentFrame(new ChooseForum(forums, type));
        }
          else
          {
              pass1Field.setText("");
              pass2Field.setText("");
              errorLabel.setText("Passwords do not match.");
          }
    }//GEN-LAST:event_registerButActionPerformed

    private void backButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButActionPerformed
     FrameHandler.getFrameHandler().changeToOtherTypeOfFrame(FrameHandler.NOTOOLBAR);//opens defualt NOTOOLBAR frame, which is the Welcome Frame
    }//GEN-LAST:event_backButActionPerformed

    private void CheckAvailButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CheckAvailButActionPerformed
        ButtonsFunctions.getButtonsFunctions().processCheckAvailability(userField.getText());
    }//GEN-LAST:event_CheckAvailButActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CheckAvailBut;
    private static javax.swing.JLabel availLabel;
    private javax.swing.JButton backBut;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPasswordField pass1Field;
    private javax.swing.JLabel pass1Label;
    private javax.swing.JPasswordField pass2Field;
    private javax.swing.JLabel pass2Label;
    private javax.swing.JButton registerBut;
    private javax.swing.JTextField userField;
    private javax.swing.JLabel userLabel;
    // End of variables declaration//GEN-END:variables

}
